package com.boot_reggie.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot_reggie.Dao.SetmealMapper;
import com.boot_reggie.Service.SetmealService;
import com.boot_reggie.common.CustomException;
import com.boot_reggie.domain.*;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.CustomSQLExceptionTranslatorRegistrar;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceimpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishServiceimpl setmealDishServiceimpl;

    @Autowired
    private CateServiceimpl cateServiceimpl;
    @Autowired
    private Dishserviceimpl dishserviceimpl;
    @Override
    public void saves(SetmealDto setmealDto) {
        this.save(setmealDto);
        Long setId = setmealDto.getId();
        setmealDto.getSetmealDishes().stream().map(item->{
            item.setSetmealId(setId);
            return item;
        }).collect(Collectors.toList());
        System.out.println(setmealDishServiceimpl.saveBatch(setmealDto.getSetmealDishes()));
    }

    @Override
    public Page<SetmealDto> pages(int page, int size, String name) {
        Page<Setmeal> page1 = new Page<>(page,size);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<Setmeal>();
        queryWrapper.like(name != null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        this.page(page1,queryWrapper);
        List<Setmeal> records = page1.getRecords();
        Page<SetmealDto> pageInfo = new Page<>(page,size);
        BeanUtils.copyProperties(page1,pageInfo);
         List<SetmealDto> list =  records.stream().map(item->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long id = item.getCategoryId(); //获取分类id
            Category category = cateServiceimpl.getById(id);
            if(category.getId() != null){
                String caregoryName = category.getName();
                setmealDto.setCategoryName(caregoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
         pageInfo.setRecords(list);
        return pageInfo;
    }

    @Override
    public void removeWithDish(List<Long> ids)  {
//        查村套餐是哦否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int cont = this.count(queryWrapper);

        if(cont > 0){
            throw new CustomException("套餐正在售卖中不能删除");
        }
//        可以删除
        this.removeByIds(ids);
//删除套餐菜品表
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishServiceimpl.remove(lambdaQueryWrapper);
    }

    @Override
    @Transactional
    public void updateWithSetmeal(int status, List<Long> ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        Setmeal setmeal = new SetmealDto();
        setmeal.setStatus(status);
        this.update(setmeal,queryWrapper);
    }

    @Override
    @Transactional
    public SetmealDto selectID(Long id) {
        Setmeal setmeal = this.getById(id);
//        根据套餐id查找名下的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null,SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDish = setmealDishServiceimpl.list(queryWrapper);
        SetmealDto setmealDto = new SetmealDto();
//        将套餐拷贝给dto
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDish);
        return setmealDto;
    }
//修改数据
    @Override
    public void updateDate(SetmealDto setmealDto) {
        if(setmealDto == null){
            throw  new CustomException("参数null");
        }
//        先修改套餐
        this.updateById(setmealDto);
//        批量删除套餐中的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
//        将带有套餐id的菜品先删除
        queryWrapper.eq(setmealDto.getId() != null,SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishServiceimpl.remove(queryWrapper);
//        给数据赋值套餐id
        setmealDto.getSetmealDishes().stream().map(item->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
//        循环添加
        setmealDishServiceimpl.saveBatch(setmealDto.getSetmealDishes());
    }


}
